import { DynamicModule, Module } from '@nestjs/common';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { GatewayController } from './gateway.controller';

@Module({})
export class GatewayModule {
  static register(services: { name: string; options: any }[]): DynamicModule {
    return {
      module: GatewayModule,
      imports: [
        ClientsModule.register(
          services.map(service => ({
            name: service.name,
            transport: Transport.TCP,
            options: service.options,
          })),
        ),
      ],
      controllers: [GatewayController],
      providers: [
        {
          provide: 'SERVICES_CONFIG',
          useValue: services,
        },
      ],
    };
  }
}